using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using BitMango;
using BitMango.Diagnostics;
using UnityEngine;

public class RemoteConfigDataModel : MonoBehaviour
{
    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public event Action<bool> OnDownloadComplete;

    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public event Action<bool> OnFetchComplete;

    public void Init()
    {
        BitMango.Diagnostics.Debugger.Log("RemoteConfig Init!");
        if (Platform.CheckAndFixedFirebase)
        {
            this.InitializeFirebase();
        }
        else
        {
            UnityEngine.Debug.LogError("Could not resolve all Firebase dependencies: false");
        }
        Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
    }

    private void InitializeFirebase()
    {
        BitMango.Diagnostics.Debugger.Log("RemoteConfig configured and ready!");
        this.isFirebaseInitialized = true;
    }


    private void FetchComplete(Task fetchTask)
    {
        if (fetchTask.IsCanceled)
        {
            UnityEngine.Debug.Log("Fetch canceled.");
        }
        else if (fetchTask.IsFaulted)
        {
            UnityEngine.Debug.Log("Fetch encountered an error.");
        }
        else if (fetchTask.IsCompleted)
        {
            UnityEngine.Debug.Log("Fetch completed successfully!");
        }
    }

    public void DisplayData()
    {
    }

    public void DownloadFileAsync()
    {
    }

    public static string CalculateMD5(string filename)
    {
        string result;
        using (MD5 md = MD5.Create())
        {
            using (FileStream fileStream = File.OpenRead(filename))
            {
                byte[] value = md.ComputeHash(fileStream);
                result = BitConverter.ToString(value).Replace("-", string.Empty).ToLowerInvariant();
            }
        }
        return result;
    }

    private static bool VersionCheck(string newVersion, string currentVersion)
    {
        UnityEngine.Debug.Log("Version Check A : " + newVersion + " , B : " + currentVersion);
        if (newVersion == currentVersion)
        {
            UnityEngine.Debug.Log("Version Check passed");
            return true;
        }
        UnityEngine.Debug.Log("Version Check failed");
        return false;
    }

    protected bool isFirebaseInitialized;

    protected CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

    public static RemoteConfigDataModel s_Instance = new RemoteConfigDataModel();

    private class WaitForTaskCompletion : CustomYieldInstruction
    {
        public WaitForTaskCompletion(Task task)
        {
            this.operationInProgress = true;
            this.task = task;
        }

        protected void DisplayStorageException(Exception exception)
        {
        }

        public override bool keepWaiting
        {
            get
            {
                if (this.task.IsCompleted)
                {
                    this.operationInProgress = false;
                    if (this.task.IsFaulted)
                    {
                        this.DisplayStorageException(this.task.Exception);
                    }
                    return false;
                }
                return true;
            }
        }


        private Task task;

        private bool operationInProgress;
    }
}
